<?php
namespace app\api\controller\order;


use app\api\controller\wanlshop\Refund;
use app\common\controller\Api;
use app\api\model\wanlshop\Order as ShopOrder;
use app\api\model\wanlshop\Refund as ShopRefund;
use app\api\model\wanlshop\RefundLog;
use app\api\model\wanlshop\Pay;
use app\api\model\wanlshop\OrderGoods;
use think\Db;
use think\Log;
use think\Request;

/**
 * WanlShop订单接口
 */
class Order extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function xiu(Request $request){
        $id = $request->get('id');
        // 更新订单状态
            $result = Db::name('wanlshop_order')
                ->where('id', $id)->update([
                    'state' => '7'
                ]);
            $order = ShopOrder::with(['pay','orderGoods'])->find($id);
            $data = [
                'user_id'=>$order->user_id,
                'shop_id'=>$order->shop_id,
                'order_id'=>$order->id,
                'order_pay_id'=>$order->pay->id,
                'order_type'=>'0',
                'goods_ids'=>$order->orderGoods->id,
                'expressType'=>'0',
                'price'=>$order->pay->price,
                'type'=>'0',
                'reason'=>'0',
                'refund_content'=>'因没走后台退款，使用后台修改状态按钮退款',
                'state'=>'4',
                'agreetime'=>time(),
                'closingtime'=>time(),
                'status'=>'normal'
            ];

            //加入退款记录
            $shopRefund = ShopRefund::create($data);
            $Log = [
                'user_id'=>$order->user_id,
                'refund_id'=>$shopRefund->id,
                'type'=>'2',
                'shop_id'=>$order->shop_id,
                'content'=>'因客户投诉，交由微信支付后台退款'
            ];
            RefundLog::create($Log);
            Pay::where('order_no',$order->order_no)->update([
                'pay_state' => '2'
            ]);
            if ($result !== false) {
                return $this->success('修改成功');
            }
    }
}